﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Ink;
using System.Threading;
using UMLTouch.DomainRecognition.Engines;
using UMLTouch.DomainRecognition.SegmentationAlgorithms;
using UMLTouch.Core.Entities;
using UMLTouch.DomainRecognition;
using System.Windows.Threading;
using UMLTouch.DomainRecognition.ShapeRecognition;
using UMLTouch.DomainRecognition.Factory;
using UMLTouch.Domain.ClassDiagram;

namespace UMLTouch.UI.Main
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        IDomainRecognition domainRecognition;
        RecognitionEngine _engine;

        //readonly object _locker = new object();

        public Window1()
        {

            InitializeComponent();

            domainRecognition = new ClassDiagramRecognition();
            _engine = domainRecognition.GetRecognitionEngine();

            this.Closed += new EventHandler(Window1_Closed);
            _engine.OnRecognizedItem += new RecognitionEngine.RecognizedItem(_engine_OnRecognizedItem);

            //segmentOutput = new DebugEngine<SegmentedStroke>();
            //intersectionsOutput = new DebugEngine<Intersection>();
            //_engine.SegRec.AddNextEngine(segmentOutput);
            //_engine.ShaRec.AddIntersectionOutput(intersectionsOutput);

            //new Thread(() =>
            //    {

            //        while (true)
            //        {
            //            List<Segment> segs = null;
            //            Intersection inter = null;

            //            lock (_locker)
            //            {
            //                if (segmentOutput.Values.Count > 0)
            //                {
            //                    segs = segmentOutput.Values.Dequeue().Segments;

            //                    Dispatcher.BeginInvoke(
            //                    DispatcherPriority.Background,

            //                    new Action(() =>
            //                    {
            //                        foreach (var seg in segs)
            //                        {
            //                            System.Windows.Ink.Stroke s = new System.Windows.Ink.Stroke(seg.toStylusStroke().StylusPoints);
            //                            s.DrawingAttributes.Color = Colors.CornflowerBlue;
            //                            ic_ink.Strokes.Add(s);
            //                        }
            //                    }));

            //                }

            //                if (intersectionsOutput.Values.Count > 0)
            //                {
            //                    inter = intersectionsOutput.Values.Dequeue();
            //                    Dispatcher.BeginInvoke(
            //                    DispatcherPriority.Background,
            //                    new Action(() =>
            //                    {
            //                        DrawPoint(inter.IntersectionPoint, Colors.Red);
            //                    }));
            //                }


            //            }

            //            Thread.Sleep(20);
            //        }

            //    }
            //    ) { IsBackground = true }.Start();

        }

        void _engine_OnRecognizedItem(Item recognizedItem)
        {
            IShapePattern p = recognizedItem.Shapes.First().ShapePattern;
            if (p != null)
            {
                Console.WriteLine(p.ToString());
                AddeStrokesRange(recognizedItem.BaseStrokes.ToList(),Colors.Green);
            }
            else
            {
                AddeStrokesRange(recognizedItem.BaseStrokes.ToList(),Colors.Red);
            }

            
            
        }


        private void AddeStrokesRange(IEnumerable<System.Windows.Ink.Stroke> strokes,Color color)
        {
            Dispatcher.BeginInvoke(
                          DispatcherPriority.Background,
                          new Action(() =>
                          {
                              foreach (var item in strokes)
                              {
                                  item.DrawingAttributes.Color = color;
                              }
                              //ic_ink.Strokes.Add(new StrokeCollection(strokes));
                          }));
            
        }

        void Window1_Closed(object sender, EventArgs e)
        {
            _engine.Dispose();
        }



        private void InkCanvas_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
        {

            UMLTouch.Core.Entities.Stroke s = new UMLTouch.Core.Entities.Stroke(e.Stroke);

            //ic_ink.Strokes.Remove(e.Stroke);

            _engine.InputItem(s);
        }

        private Point DrawPoint(Point p, Color c)
        {
            double w = 10d;
            double h = 10d;

            Ellipse ellipse = new Ellipse();

            ellipse.SetValue(InkCanvas.LeftProperty, p.X - w / 2);

            ellipse.SetValue(InkCanvas.TopProperty, p.Y - h / 2);

            ellipse.Width = w;

            ellipse.Height = h;

            ellipse.Fill = new SolidColorBrush(c);

            ic_ink.Children.Add(ellipse);
            return p;
        }



    }
}
